//
//  HomePageMenuView.m
//  ShowYork
//
//  Created by hxh on 2021/4/29.
//  Copyright © 2021 hxh. All rights reserved.
//


#import "HomePageMenuView.h"
#import "HomePageMenuItemModel.h"
#import "HomeMenuItemCell.h"

typedef void(^doneBlock)(XHMenuItemType type);

@interface HomePageMenuView ()<UITableViewDelegate,UITableViewDataSource,HomeMenuItemCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,copy)NSArray *itemArray;
@end
    
@implementation HomePageMenuView


- (instancetype)initWithItemArray:(NSArray *)itemArray completeBlock:(void (^)(XHMenuItemType type))completeBlock{
    if (self = [super init]) {
        self.itemArray = itemArray;
        [self setUI];
        if (completeBlock) {
            self.done = ^(XHMenuItemType type) {
                completeBlock(type);
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.4);

    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = RGBA(248, 248, 248, 1);
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorColor = [UIColor clearColor];
    [self addSubview:self.tableView];

    CGFloat maxHeight = 8 + self.itemArray.count * 60 + 60 + GK_SAVEAREA_BTM;
    self.tableView.frame = CGRectMake(0, kScreenH, kScreenW, maxHeight);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
}

- (void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.cl_y = kScreenH - self.tableView.cl_height;
    }];
}
- (void)dismiss{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.cl_y = kScreenH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.itemArray.count == 0) {
        return 0;
    }
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.itemArray.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeMenuItemCell *cell = [HomeMenuItemCell cellWithTableView:tableView];
    if (indexPath.section == 1) {
        HomePageMenuItemModel *itemModel = [[HomePageMenuItemModel alloc]init];
        itemModel.mainTitle = @"取消";
        itemModel.subTitle = @"";
        itemModel.itemType = XHMENUITEMCANCEL;
        [cell setItemModel:itemModel];
    }else{
        HomePageMenuItemModel *itemModel = self.itemArray[indexPath.item];
        itemModel.isHide = indexPath.item == (self.itemArray.count - 1);
        [cell setItemModel:self.itemArray[indexPath.item]];
    }
    cell.delegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60;
    }else{
        return 60+GK_SAVEAREA_BTM;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 8.0f;
    }
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.tableView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(14, 14)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.tableView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.tableView.layer.mask = maskLayer;
    
}

#pragma mark - HomeMenuItemCellDelegate
- (void)showMenuItem:(HomePageMenuItemModel *)itemModel{
    if (itemModel.itemType == XHMENUITEMCANCEL) {
        [self dismiss];
    }else{
        if (self.done) {
            self.done(itemModel.itemType);
        }
        [self dismiss];
    }
    
}

@end
